package com.som.service.excel.data3.state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.som.bean.data3.IpAddressInfo;
import com.som.bean.data3.IpMaskInfo;
import com.som.bean.data3.IpSectionInfo;
import com.som.console.CfgEnum;

/**
 * 将IP集体转换为其他形式的数据
 * 
 * @author liujun
 * @date 2016年4月12日
 * @verion 0.0.1
 */
public class ParseIpStateImp implements ParseStateInf<List<IpAddressInfo>>
{
	/**
	 * 上下文对象
	 */
	private StateContext context;

	public ParseIpStateImp(StateContext context)
	{
		this.context = context;
	}

	public List<IpAddressInfo> parseIp(List<IpAddressInfo> param)
	{
		return param;
	}

	public List<IpSectionInfo> parseIpSection(List<IpAddressInfo> param)
	{
		if (null != param && !param.isEmpty())
		{
			List<IpSectionInfo> result = new LinkedList<IpSectionInfo>();

			// 按信息进行归类
			Map<String, List<IpAddressInfo>> mapip = new LinkedHashMap<String, List<IpAddressInfo>>();

			List<IpAddressInfo> ipAddrList = null;

			String ipKey = null;

			for (IpAddressInfo ipaddressinfo : param)
			{
				ipKey = ipaddressinfo.getMsginfo() + "_" + ipaddressinfo.getIpaddress().substring(0, ipaddressinfo.getIpaddress().lastIndexOf("."));
				// 如果当前map不包括已经存在的信息，则创建一个新的集体用于记录
				if (!mapip.containsKey(ipKey))
				{
					ipAddrList = new ArrayList<IpAddressInfo>();
				} else
				{
					ipAddrList = mapip.get(ipKey);
				}

				ipAddrList.add(ipaddressinfo);

				mapip.put(ipKey, ipAddrList);
			}

			// 针对信息相同的IP来进行取得开始与结束IP
			Iterator<Entry<String, List<IpAddressInfo>>> iterIpAdd = mapip.entrySet().iterator();

			Entry<String, List<IpAddressInfo>> ipItemEntry = null;

			List<IpAddressInfo> ipList = null;
			String msgInfo = null;

			IpAddressInfo comp1 = null;
			IpAddressInfo comp2 = null;

			IpAddressInfo startIpAddress = null;

			IpSectionInfo ipSection = null;

			while (iterIpAdd.hasNext())
			{
				ipItemEntry = iterIpAdd.next();
				msgInfo = ipItemEntry.getKey();
				ipList = ipItemEntry.getValue();

				if (null != ipList)
				{
					Collections.sort(ipList);

					// 至少有两个才进行比较，
					if (ipList.size() > 1)
					{

						for (int i = 1; i < ipList.size(); i++)
						{
							if (i == 1)
							{
								comp1 = ipList.get(0);
								comp2 = ipList.get(i);
							} else
							{
								comp2 = ipList.get(i);
							}

							// 如果当前IP为连接的是否为连接的IP
							if (this.compIpcon(comp1.getIpaddress(), comp2.getIpaddress()))
							{
								if (null == startIpAddress)
								{
									startIpAddress = comp1;
								}

								// 检查是为最后一次比较
								if (i == ipList.size() - 1)
								{
									ipSection = new IpSectionInfo();
									ipSection.setIpaddress(startIpAddress.getIpaddress());
									ipSection.setIpEnd(comp2.getIpaddress());
									ipSection.setMsginfo(msgInfo.substring(0, msgInfo.indexOf("_")));
									result.add(ipSection);

									startIpAddress = null;
								} else
								{
									comp1 = comp2;
								}

							} else
							{
								ipSection = new IpSectionInfo();
								ipSection.setIpaddress(comp1.getIpaddress());
								ipSection.setMsginfo(msgInfo.substring(0, msgInfo.indexOf("_")));
								result.add(ipSection);

								ipSection = new IpSectionInfo();
								ipSection.setIpaddress(comp2.getIpaddress());
								ipSection.setMsginfo(msgInfo.substring(0, msgInfo.indexOf("_")));

								result.add(ipSection);
							}

						}
					}
					// 否则直接输出
					else
					{
						ipSection = new IpSectionInfo();
						ipSection.setIpaddress(ipList.get(0).getIpaddress());
						ipSection.setIpEnd(ipList.get(0).getIpEnd());
						ipSection.setMsginfo(msgInfo.substring(0, msgInfo.indexOf("_")));

						result.add(ipSection);
					}

				}
			}
			return result;
		}

		return null;
	}

	/**
	 * 检查两个IP是否为连接的IP
	 * 
	 * @param startIp
	 *            开始IP
	 * @param endIP
	 *            结束IP
	 * @return true 是连接的，false 不是连接的
	 */
	private boolean compIpcon(String startIp, String endIP)
	{
		if (!StringUtils.isEmpty(startIp) && !StringUtils.isEmpty(endIP))
		{
			startIp = startIp.trim();
			endIP = endIP.trim();
			int startValueIP = Integer.parseInt(startIp.substring(startIp.lastIndexOf(".") + 1));
			int endValueIP = Integer.parseInt(endIP.substring(endIP.lastIndexOf(".") + 1));

			// 前面IP的尾数加1，如果等于后面的
			if ((startValueIP + 1) == endValueIP)
			{
				return true;
			}

		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public List<IpMaskInfo> parseIpMask(List<IpAddressInfo> param)
	{
		// 第一步将数据转换为ip段，
		List<IpSectionInfo> listSection = this.parseIpSection(param);

		// 设置当前的转换模式为从IP段转为掩码
		this.context.setCurrParse(CfgEnum.SYSTEM_FORMAT_TYPE_IPSECTION.getCfgMsg());
		// 再将IP段转换为掩码
		List<IpMaskInfo> listMask = this.context.getCurrParse().parseIpMask(listSection);

		return listMask;
	}

	public static void main(String[] args)
	{
		StateContext context = new StateContext();

		ParseIpStateImp ipState = new ParseIpStateImp(context);

		List<IpAddressInfo> param = new LinkedList<IpAddressInfo>();

		for (int i = 1; i < 17; i++)
		{
			IpAddressInfo ip = new IpAddressInfo();
			ip.setIpaddress("192.168.1." + i);
			ip.setMsginfo("你好");
			param.add(ip);
		}

		for (int i = 1; i < 9; i++)
		{
			IpAddressInfo ip = new IpAddressInfo();
			ip.setIpaddress("192.168.2." + i);
			ip.setMsginfo("你好");
			param.add(ip);
		}

		for (int i = 1; i < 3; i++)
		{
			IpAddressInfo ip = new IpAddressInfo();
			ip.setIpaddress("192.168.3." + i);
			ip.setMsginfo("你好");
			param.add(ip);
		}

		for (int i = 1; i < 2; i++)
		{
			IpAddressInfo ip = new IpAddressInfo();
			ip.setIpaddress("192.168.4." + i);
			ip.setMsginfo("你好");
			param.add(ip);
		}

		List<IpSectionInfo> ipSectionList = ipState.parseIpSection(param);
		System.out.println(ipSectionList);

		List<IpMaskInfo> ipMaskList = ipState.parseIpMask(param);
		System.out.println(ipMaskList);
	}
}
